﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Pages.Controls;

namespace Rio.Framework.Pages.ChildWindows
{
    public partial class OcrConflictChildWindow : ChildWindow
    {

        private List<OcrItem> OCRConflictIndex;
        public List<OcrItem> NewOCRConflictIndex {private set; get; }

        public OcrConflictChildWindow(List<OcrItem> p_oConflictIndexes)
        {
            InitializeComponent();
            this.KeyUp += MessageBoxChildWindow_KeyUp;
            OCRConflictIndex = p_oConflictIndexes;
            NewOCRConflictIndex = new List<OcrItem>();
            LoadIndexControl();
        }

        private void MessageBoxChildWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void LoadIndexControl()
        {
            foreach (OcrItem l_oIndex in OCRConflictIndex)
            {
                if (l_oIndex.OcrValueList.Count() > 1)
                {
                    OCRConflictIndexControl l_oIndexControl = new OCRConflictIndexControl(l_oIndex.OcrIndex, new List<string>(l_oIndex.OcrValueList));
                    MainPanel.Children.Add(l_oIndexControl);
                }
            }
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {

            Dictionary<long, string> l_oIndexList = new Dictionary<long, string>();
            foreach (UIElement item in MainPanel.Children)
            {
                OCRConflictIndexControl l_oIndexControl = (OCRConflictIndexControl)item;

                List<string> L_oOcrValue = new List<string>() { l_oIndexControl.IndexValues.SelectedItem as string };

                Index L_oOcrIndex = l_oIndexControl.Index;

                NewOCRConflictIndex.Add(new OcrItem() { OcrIndex = L_oOcrIndex, OcrValueList = L_oOcrValue });
                //((List<string>)OCRConflictIndex.Where(x => x.OcrIndex.Indentifier == l_oIndexControl.Index.Indentifier).First().OcrValueList).ToList<string>().Clear();
                //((List<string>)OCRConflictIndex.Where(x => x.OcrIndex.Indentifier == l_oIndexControl.Index.Indentifier).First().OcrValueList).ToList<string>().Add(l_oIndexControl.IndexValues.SelectedItem as string);
                //OCRConflictIndex.Where(x => x.OcrIndex.Indentifier == l_oIndexControl.Index.Indentifier).First().OcrValueList = new List<string>(){l_oIndexControl.IndexValues.SelectedItem as string};
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        //Dictionary<Dictionary<long, string>, List<String>> m_oConflictIndexes;
        //public Dictionary<long, string> OCRConflictIndex { private set; get; }

        //public OcrConflictChildWindow(Dictionary<Dictionary<long, string>, List<String>> p_oConflictIndexes)
        //{
        //    InitializeComponent();
        //    m_oConflictIndexes = p_oConflictIndexes;
        //    LoadIndexControl();
        //}

        //private void LoadIndexControl()
        //{
        //    foreach (Dictionary<long, string> l_oIndex in m_oConflictIndexes.Keys)
        //    {
        //        OCRConflictIndexControl l_oIndexControl = new OCRConflictIndexControl(l_oIndex, m_oConflictIndexes[l_oIndex]);
        //        MainPanel.Children.Add(l_oIndexControl);
        //    }
        //}
        
        
        //private void OKButton_Click(object sender, RoutedEventArgs e)
        //{

        //    Dictionary<long, string> l_oIndexList = new Dictionary<long, string>();
        //    foreach (UIElement item in MainPanel.Children)
        //    {
        //        OCRConflictIndexControl l_oIndexControl = (OCRConflictIndexControl)item;
        //        l_oIndexList.Add(l_oIndexControl.Index.Keys.First(), l_oIndexControl.IndexValues.SelectedItem as string);
        //    }
        //    OCRConflictIndex = l_oIndexList;
        //    this.DialogResult = true;
        //}

        //private void CancelButton_Click(object sender, RoutedEventArgs e)
        //{
        //    this.DialogResult = false;
        //}
    }
}

